#ifndef SLOTCONN_H
#define SLOTCONN_H

#include "progconn.h"
#include "slotguts.h"
#include "slotspin.h"

#define APP1		(uint8)'S'
#define APP2		(uint8)'M'

#define GAMEID			6	// Slots gameId

class SlotConnection : public ProgressiveConnection
{
	public:
		SlotConnection(ProgressiveServer*server, SOCKET client);
		~SlotConnection();

		void msgStart();
		void msgAdd(uint8 Byte);
		uint8* msgGetBuf();
		void msgReleaseBuf(int32 Amt);
		void msgEnd();

        enum ClassConstants
        {
            MAX_SPINS_PER_MSG = 5,
            SINGLE_SPIN_RESPONSE = 1,
            FIRST_SPIN_RESPONSE = 2,
            MIDDLE_SPIN_RESPONSE = 3,
            LAST_SPIN_RESPONSE = 4
        };

	protected:
		bool8	getResponse();

		void	sendStopMsg();
		void	sendMalfunctionMsg();
		void	sendLoginMalfunctionMsg();

		bool8	parseAnsiCasinoLogin(int32 stdReq, int32* Err);
		bool8	parseUnicodeCasinoLogin(int32 stdReq, int32* Err);
		bool8   parseReloadDenomination();	// CSN-14779: reload with a new denomination
		void	removeCasinoSpecific();

		int32	encode(uint8*, uint8*, int32);
		int32	decode(uint8*, uint8*, int32);

		bool8   trackGetBalance();
		bool8	trackGetPlayerLimits();
		bool8	trackPreferedMachSubId(uint8 MachId, uint16 preferedCreditValue, const PID& pid, bool8 forMoney, uint16* MachSubId);
		int32	trackValidateLogin();
		int32	trackValidateSession();

		// CSN-7075: New Authentication
		void	trackLogoutSession();

        bool8 handleGutsMsg();
        bool8 handleGutsReelsMsg();
        bool8 handleGutsPayoutsMsg();
        bool8 handleGutsPaylinesMsg();
        bool8 handleGutsIconNameMsg();
        bool8 handleGutsDenominationsMsg();

        bool8 handleSavedGameMsg();
        bool8 handleJackpotMsg();
        bool8 handleLoginMsg(bool isReload = false);

		bool8 handleAnsiLoginMsg();
		bool8 handleUnicodeLoginMsg();
		bool8 HandleReloadDenomination();	// CSN-14779: change denomination

        bool8 handleSubgameMsg();
        bool8 handleSpinMsg();
        void executeSpinMsg(uint8 bet, uint8 *lines, uint8 lineCount);
        void packSpins();
        bool8 handleBonusGameMsg();
        bool8 handleUnknownMsg();
        bool8 handleGetNextSpinResponseMsg();
        bool8 handleGetBalance();
		bool8 handlePreferedMachSubId();

        void SendInt32(int32 data);
        void SendUint16(uint16 data);
        void SendUint8(uint8 data);
        uint8 SendString( char* s );

		int32 getProgressiveFixedAmountMultiplier();

		void allocateXMLTranslator( XMLTranslator** target );

	protected:
		int32 loggedOn;

		SlotGuts* guts;
		pInfo info;
		int32 msglen;
		uint8 msgbuf[CONNECT_OBUF];
        bool8 m_bHasMoreSpinsToPack;
		int32 m_nDecodedMsgLen;			// kharmon.  08-17-05.  #4962, #5116.  Keep track of received message length, after decoding.

        //ProgressiveConnection abstract function required to be implemented by subclass
        virtual bool8 getForRealFlag();

	protected:
		int32 creditsToCents(int32 cred);
		int32 centsToCredits(int32 cents);

	// [AO 2008-01-16] Refactoring for HA
	private:
		bool ValidateSession();
};

inline
int32
SlotConnection::getProgressiveFixedAmountMultiplier()
{
	if (guts && guts->getInfo() && 
		guts->getInfo()->machineSubID &&
		guts->getInfo()->creditValue)
	{
		return (int32)(guts->getInfo()->creditValue / guts->getInfo()->machineSubID);
	}

	return 1;
}

#endif
